package com.kerrykidz.scmstock.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.scminvoicing.dao.model.ScmOutStgEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmOutstgPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPrdStgEntity;
import com.kerrykidz.scmstock.service.IStockGoodsOutService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: StockGoodsOutController
 * @description: 商品出库服务类
 * @author: huanjiang
 * @date: 2014年8月21日 下午4:38:54
 * @version: V1.0
 * 
 */
@Controller
@RequestMapping("StockGoodsOut")
public class StockGoodsOutController {

	@Autowired
	private IStockGoodsOutService stockGoodsOutService;

	@Autowired
	private ICommonService commonService;

	/**
	 * @throws RDPException
	 * 
	 * 
	 * @Title: toStockGoodsOut
	 * @Description: 跳转到商品出库页面
	 * @date: 2014年8月21日
	 * @return: ModelAndView
	 * @throws
	 * 
	 */
	@RequestMapping("toStockGoodsOut")
	public ModelAndView toStockGoodsOut() throws RDPException {
		ModelAndView mav = new ModelAndView("stock/stock_goodsOut");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.toUpperCase().indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				mav.addObject("branList", branList);
				for (GblSecBranEntity gblSecBranEntity : branList) {
					brandId = gblSecBranEntity.getPkBranId();
					if (brandId.toUpperCase().indexOf("WX") != -1) {
						mav.addObject("branId", brandId);
					}
				}
			} else {
				branList = commonService.getBranListForStock(brandId);
				mav.addObject("branId", brandId);
				mav.addObject("branList", branList);
			}
		}
		return mav;
	}

	/**
	 * 
	 * @Title: toStockAddGoodsOut
	 * @Description: 跳转到商品出库_新增出库页面
	 * @date: 2014年8月21日
	 * @return: ModelAndView
	 * @throws
	 */
	@RequestMapping("toStockAddGoodsOut")
	public ModelAndView toStockAddGoodsOut() {
		UserInfo userInfo = UserUtil.getUserFromSession();
		String storeId = userInfo.getBranId();
		String createTime = new SimpleDateFormat("yyyy-MM-dd")
				.format(new Date());
		String stockId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.CK,
				userInfo.getAbbrCode());

		ModelAndView mav = new ModelAndView("stock/stock_addGoodsOut");
		mav.addObject("userName", userInfo.getName());
		mav.addObject("createTime", createTime);
		mav.addObject("stockId", stockId);
		mav.addObject("storeId", storeId);
		mav.addObject("usrId", userInfo.getUserId());
		mav.addObject("branName", userInfo.getBranName());
		return mav;
	}

	/**
	 * 
	 * @Title: findscmOutStg
	 * @Description: 商品出库中出库单分页显示
	 * @param: scmOutStg
	 * @date: 2014年8月20日
	 * @return: ScmOutStgEntity 出库记录表
	 * @throws RDPException
	 * 
	 */
	@RequestMapping(value = "findscmOutStg", method = RequestMethod.POST)
	@ResponseBody
	public ScmOutStgEntity findscmOutStg(ScmOutStgEntity scmOutStg)
			throws RDPException {
		return this.stockGoodsOutService.findscmOutStg(scmOutStg);
	}

	@RequestMapping("rollBackForStockOut")
	@ResponseBody
	public String rollBackForStockOut(String pkOutstgId) throws RDPException {
		ScmPrdStgEntity scmPrdStg = new ScmPrdStgEntity();
		UserInfo userInfo = UserUtil.getUserFromSession();
		scmPrdStg.setModiBy(userInfo.getUserId());
		boolean success = stockGoodsOutService.rollBackForStockOut(pkOutstgId,
				scmPrdStg);
		if (success) {
			return Constants.SUCCESS;
		}
		return Constants.FAILURE;
	}

	/**
	 * 
	 * @Title: stockGoodsOut
	 * @Description: 出库
	 * @param: scmOutStg
	 * @param: contents 包含出库商品数据字符串
	 * @date: 2014年8月27日
	 * @return: void
	 * @throws RDPException
	 * 
	 */
	@RequestMapping(value = "doStockGoodsOut", method = RequestMethod.POST)
	@ResponseBody
	public String doStockGoodsOut(ScmOutStgEntity scmOutStg, String contents,
			HttpSession session) {
		try {
			scmOutStg.setDelFlag("0");
			try {
				String fileName = URLDecoder.decode(scmOutStg.getFkAttFile(),
						"UTF-8");
				scmOutStg.setFkAttFile(fileName);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			stockGoodsOutService.doStockGoodsOut(scmOutStg, contents);
			return Constants.SUCCESS;
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
	}

	/**
	 * 
	 * @Title: toStockGoodsOutDetail
	 * @Description: 跳转
	 * @param pkOutstgId
	 * @return
	 * @throws RDPException
	 * @date: 2014年8月29日
	 * @return:ModelAndView
	 */
	@RequestMapping("toStockGoodsOutDetail")
	public ModelAndView toStockGoodsOutDetail(String pkOutstgId)
			throws RDPException {
		ScmOutStgEntity scmOutStg = stockGoodsOutService
				.findScmOutStgDetail(pkOutstgId);
		ModelAndView mav = new ModelAndView("stock/stock_goodsOutDetail");
		String modiTime = new SimpleDateFormat("yyyy-MM-dd").format(scmOutStg
				.getModiTime());
		mav.addObject("scmOutStg", scmOutStg);
		mav.addObject("key", Constants.SYS_BASE_PATH_STOCK);
		mav.addObject("modiTime", modiTime);
		return mav;
	}

	/**
	 * 
	 * @Title: getProductListForDetail
	 * @Description: 获得出库清单中商品列表信息
	 * @param fkOutstgId
	 *            出库单id
	 * @param fkBranId
	 *            门店id
	 * @throws RDPException
	 * @date: 2014年9月1日
	 * @return:List<GblScmPrdEntity>
	 */
	@RequestMapping(value = "getProductListForDetail", method = RequestMethod.POST)
	@ResponseBody
	public List<ScmOutstgPrdEntity> getProductListForDetail(String fkOutstgId) throws RDPException {
		return stockGoodsOutService.getProductListForDetail(fkOutstgId);
	}

}
